home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
1035
/
1035.xpi
/
chrome
/
1clickweather.jar
/
content
/
1clickweather
/
js
/
data
/
search.js
< prev
next >
Wrap
Text File
|
2008-10-05
|
6KB
|
202 lines
// � 2005 The Weather Channel Interactive, Inc. All Rights Reserved.
var GlobalSearch = null;
function sendupdate(str) {
Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService).notifyObservers(null, "searchupdate", str);
}
function loadSearch(){
try{
jsInclude("chrome://1clickweather/content/js/utils/definitions.js");
jsInclude("chrome://1clickweather/content/js/utils/datadumper.js");
jsInclude("chrome://1clickweather/content/js/utils/appconstants.js");
jsInclude("chrome://1clickweather/content/js/utils/network.js");
try{
// fire up the config system.
// loadconfig.js can be included where ever you need access to config variables.
// it will pop GlobalUserConfig and GlobalAppConfig into existance
jsInclude("chrome://1clickweather/content/js/config/loadconfig.js");
}catch(e){
debug("loadconfig: " + e);
}
GlobalSearch = new oSearch();
try{
/*
alert(typeof(window.arguments[0]));
*/
if(typeof(window.arguments[0]) == "object"){
/*
if(window.arguments[0].data['search']){
*/
//alert("args: " + window.arguments[0].data);
document.getElementById("searchInput").value = window.arguments[0].data['search'];
fillSearchListBox();
}
}catch(e){ }
}catch(e){
debug("error loading search: " + e);
}
}
function oSearch(){
this.Search = function(location){
if(!location){
return(false);
}
// there are some cases where we get a location with the zip in ()
// so remove from the space just before the () to the end of the string
location = location.replace(/ \(.*/g, '');
var dataUrl = "";
try {
var configUtils = new ConfigUtils();
dataUrl = configUtils.getUrl(GlobalAppConfig, GlobalUserConfig, "Search"); }catch(e){
return(false);
}
dataUrl += location;
if(dataUrl){
try{
var xmlRequest = new XMLRequest();
xmlRequest.setUrl(dataUrl);
var xmlDoc = xmlRequest.Get();
// once we get the xml, parse it up
return(this.parseSearchXML(xmlDoc));
}catch(e){
debug("error in search get: " + e);
return(false);
}
}
return(false);
}
this.parseSearchXML = function(xmlDoc){
if(!xmlDoc){
return(false);
}
var node = xmlDoc.getElementsByTagName("search")[0].childNodes;
var x = 0;
var Data = {};
// loop through all the children
for(x = 0; x < node.length; x++){
try{
// if the nodes we find only have a value and no children, make them into variables
if((node[x].childNodes.length == 1) && (typeof(node[x].firstChild.nodeValue) == "string")){
var id = node[x].getAttribute("id");
Data[id] = new Array();
Data[id] = node[x].firstChild.nodeValue;
}
}
catch(e){
debug('error parsing search xml: ' + e.message);
return(false);
}
}
return(Data);
}
this.getLocationData = function(loc){
if(loc){
var dataUrl = "";
try {
var configUtils = new ConfigUtils();
dataUrl = configUtils.getUrl(GlobalAppConfig, GlobalUserConfig, "Location", loc);
}catch(e){
return(false);
}
if(dataUrl){
try{
var xmlRequest = new XMLRequest();
xmlRequest.setUrl(dataUrl);
var locDoc = xmlRequest.Get();
var country = locDoc.getElementsByTagName('ctry')[0].firstChild.nodeValue;
var dnam = locDoc.getElementsByTagName('dnam')[0].firstChild.nodeValue;
var locString = loc + "|" + country + "|" + dnam;
return(locString);
}catch(e){
// debug("error in location get: " + e);
return(false);
}
}
}else{
debug("error, invalid loc " + loc);
}
return(false);
}
}
/* fillSearchListBox() */
function fillSearchListBox(){
/* get user selection */
var listBox = document.getElementById("searchList");
var loc = document.getElementById("searchInput").value;
if(loc){
try{
while(listBox.childNodes.length > 0) listBox.removeChild(listBox.childNodes[0]);
}
catch(e){
//alert("l: " + e);
}
try{
/* Search data to selected location */
var Data = GlobalSearch.Search(loc);
for(var i in Data){
var item = document.createElement("listitem");
item.setAttribute("label", Data[i]);
item.setAttribute("value", i);
listBox.appendChild(item);
}
}catch(e){
//alert("e: " + e);
}
}else{
try{
while(listBox.childNodes.length > 0) listBox.removeChild(listBox.childNodes[0]);
}catch(e){
//alert("l: " + e);
}
}
}
/* saveSearchLocation () */
function saveSearchLocation(){
/* get listbox */
var listBox = document.getElementById("searchList");
if(listBox.selectedItem.value != null){
// get the locaton data
var locStr = GlobalSearch.getLocationData(listBox.selectedItem.value);
if(locStr){
sendupdate(locStr);
}
window.close();
}
}